<template>
  <div>
    <keep-alive>
      <transition>
        <router-view v-if="$route.meta.keepAlive" v-wechat-title="$route.meta.title"></router-view>
      </transition>
    </keep-alive>
    <transition>
      <router-view v-if="!$route.meta.keepAlive" v-wechat-title="$route.meta.title"></router-view>
    </transition>
    <!-- tabbar -->
    <p class="tabbar-placeholder" v-if="tabbarShow"></p>
    <ul class="tabbar-list" v-if="tabbarShow" ref="tabbar">
      <router-link
        :class="['tabbar-item',{'tabbar-item-active':item.path == currentPath}]"
        v-for="item in list"
        :key="item.name"
        :to="item.path"
        tag="li"
      >
        <img :src="item.path == currentPath ? item.iconActive : item.icon" />
        <span>{{ item.name }}</span>
      </router-link>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabbarShow: true,
      currentPath: this.$route.path,
      list: [
        {
          icon: require("@/assets/images/tabbar_home_normal.png"),
          iconActive: require("@/assets/images/tabbar_home_pressed.png"),
          name: "主页",
          path: "/home"
        },
        {
          icon: require("@/assets/images/tabbar_order_normal.png"),
          iconActive: require("@/assets/images/tabbar_order_pressed.png"),
          name: "订单",
          path: "/order"
        },
        {
          icon: require("@/assets/images/tabbar_equipment_normal.png"),
          iconActive: require("@/assets/images/tabbar_equipment_pressed.png"),
          name: "设备",
          path: "/equipment"
        },
        {
          icon: require("@/assets/images/tabbar_financial_normal.png"),
          iconActive: require("@/assets/images/tabbar_financial_pressed.png"),
          name: "财务",
          path: "financial"
        },
        {
          icon: require("@/assets/images/tabbar_my_normal.png"),
          iconActive: require("@/assets/images/tabbar_my_pressed.png"),
          name: "我的",
          path: "my"
        }
      ]
    };
  },
  mounted() {
    this.handleTabBar();
  },
  methods: {
    // 处理tabbar的显示隐藏
    handleTabBar() {
      this.tabbarShow = this.list.some(item => item.path == this.currentPath);
    }
  },
  watch: {
    $route(to, from) {
      this.currentPath = to.path;
      this.handleTabBar();
    }
  }
};
</script>

<style scoped>
/* 动画 */
/* .v-enter {
  opacity: 0;
  transform: translateX(100%);
}
.v-leave-to {
  opacity: 0;
  transform: translateX(-100%);
  position: absolute;
}
.v-enter-active,
.v-leave-active {
  transition: all 0.5s ease;
} */
/* tabbar */
.tabbar-placeholder {
  width: 100%;
  height: 1.306667rem;
}
.tabbar-list {
  z-index: 10;
  position: fixed;
  left: 0;
  bottom: 0;
  height: 1.306667rem;
  width: 100%;
  background-color: white;
  display: flex;
  align-items: center;
  box-shadow: 0px -3px 29px 0px rgba(102, 102, 102, 0.1);
}
.tabbar-item {
  height: 100%;
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.tabbar-item span {
  margin-top: 0.12rem;
  font-size: 0.28rem;
  color: #292929;
}
.tabbar-item-active span {
  color: #2D4D7D;
}
.tabbar-item img {
  width: 0.533333rem;
  height: 0.533333rem;
}
</style>
